chinese_units = ["小时", "分钟", "秒"]
chinese_numbers = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]


class SuggestedReadingTimeFunctions(object):
    """
    Model:
        满足建议阅读时间的所有算法逻辑

    Param:
        - env: odoo env

    Function:
        - convert_to_time_and_text: 时间换算
    """

    def __init__(self, env):
        self.env = env

    def convert_to_time_and_text(self, integer, time_uom):
        unit = time_uom.name
        number = integer
        # 根据单位计算小时、分钟和秒
        if unit == "Min":
            hours = number // 60
            minutes = number % 60
            seconds = 0
        elif unit == "S":
            hours = number // 3600
            minutes = (number % 3600) // 60
            seconds = number % 60
        else:
            return None, None

        # 转换为时钟格式
        clock_format = f"{hours:02d}:{minutes:02d}:{seconds:02d}"

        # 转换为文本格式
        time_components = [hours, minutes, seconds]
        text_format_parts = []
        for i, component in enumerate(time_components):
            if component > 0:
                if component < 10:
                    text_format_parts.append(chinese_numbers[component] + chinese_units[i])
                elif component == 10:
                    text_format_parts.append("十" + chinese_units[i])
                else:
                    ten_digit = component // 10
                    unit_digit = component % 10
                    ten_digit_str = "十" if ten_digit == 1 else chinese_numbers[ten_digit] + "十"
                    unit_digit_str = "" if unit_digit == 0 else chinese_numbers[unit_digit]
                    text_format_parts.append(ten_digit_str + unit_digit_str + chinese_units[i])

        text_format = "零".join(text_format_parts) if text_format_parts else "零秒"

        return clock_format, text_format
